//
//  TPCatesViewController.m
//  Yimei
//
//  Created by xiaowu xiaowu on 2017/10/23.
//  Copyright © 2017年 xiaowu xiaowu. All rights reserved.
//

#import "TPCatesViewController.h"
#import "XRWaterfallLayout.h"
#import "TPCasesCell.h"
#import "HXDropdownMenu.h"
#import "TPHomeModel.h"
#import "TPProductDetailViewController.h"
#import "TPCatesTagModel.h"
#import "TPSubmitProductModel.h"
@interface TPCatesViewController ()<UICollectionViewDataSource, XRWaterfallLayoutDelegate,UICollectionViewDelegate>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic,strong)HXDropdownMenu *menu;
@property (nonatomic,strong)NSMutableArray *menuModels;

@end


@implementation TPCatesViewController

-(TPSubmitProductModel *)submitModel{
    
    if(!_submitModel){
        _submitModel = [TPSubmitProductModel new];
    }
    return _submitModel;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"分类";
    
    @weakify(self);
    //创建瀑布流布局
    XRWaterfallLayout *waterfall = [XRWaterfallLayout waterFallLayoutWithColumnCount:2];
    
    //设置各属性的值
    //    waterfall.rowSpacing = 10;
    //    waterfall.columnSpacing = 10;
    //    waterfall.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    //或者一次性设置
    [waterfall setColumnSpacing:10 rowSpacing:10 sectionInset:UIEdgeInsetsMake(10, 10, 10, 10)];
    //设置代理，实现代理方法
    waterfall.delegate = self;
    /*
     //或者设置block
     [waterfall setItemHeightBlock:^CGFloat(CGFloat itemWidth, NSIndexPath *indexPath) {
     //根据图片的原始尺寸，及显示宽度，等比例缩放来计算显示高度
     XRImage *image = self.images[indexPath.item];
     return image.imageH / image.imageW * itemWidth;
     }];
     */
    //创建collectionView
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:waterfall];
    self.collectionView.backgroundColor = [UIColor colorWithHexString:@"f5f4f5"];
    [self.collectionView registerClass:[TPCasesCell class] forCellWithReuseIdentifier:@"TPCasesCell"];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    [self.view addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0.0);
        make.top.mas_equalTo(IOS7_TOP_Y + 44.0);
        make.bottom.mas_equalTo(0.0);
    }];
    
    
    _menuModels = [NSMutableArray new];
    NSArray *titles = @[@"销量",@"美容机构",@"筛选"];
    NSArray *imgs = @[@"TP_Cate_topbar_sale_icon",@"Home_city_down_arrow",@"Home_city_down_arrow"];
    
 
    
    for(NSInteger i = 0; i<3;i++){
        HXMenuModel *model = [[HXMenuModel alloc]init];
        model.type = i;
        model.nimage = ImageNamed(imgs[i]);
        model.simage = [ImageNamed(imgs[i]) imageWithGradientTintColor:TPAllBackgroundColor];
        model.title = titles[i];
        model.contents =@[];// @[@"广州博仕整形",@"广州时光医学整形",@"广州曙光医学美容医院",@"广州圣心微美医疗美容"];
        [_menuModels addObject:model];
    }
    _menu = [[HXDropdownMenu alloc]initFrame:CGRectMake(0, IOS7_TOP_Y, IPHONE_WIDTH, 44.0) toMenus:_menuModels];
    _menu.submitModel = self.submitModel;
    [self.view addSubview:_menu.view];
    [self.view bringSubviewToFront:_menu.view];
    [_menu setSubmitBlock:^{
        @strongify(self);
        [self getRequest];
    }];
    
    [self getClinicRequset];
    [self getRequest];
}
-(void)getClinicRequset{
    
    
    [[TPRequestClient sharedClient]getTemplateClinic_success:^(NSURLSessionDataTask *task, id responseObject, Responese *respone) {
        
        NSArray *array = [NSArray yy_modelArrayWithClass:[TPCatesTagModel class] json:respone.data[@"data"]];
        [_menuModels enumerateObjectsUsingBlock:^(HXMenuModel *obj, NSUInteger idx, BOOL * _Nonnull stop) {
            obj.contents = array;
        }];
        
        
    } failure:^(NSURLSessionDataTask *task, NSError *error, Responese *respone) {
        ;
    } nonetWork:^(NSURLSessionDataTask *task, NSError *error) {
        ;
    }];
}

-(void)getRequest{
    
    [self.collectionView tp_hideNotMsg];
    [[TPRequestClient sharedClient]getTemplateListCateId:self.submitModel.gc_id cityName:[XWCommon shareAppDelegate].locationCity goods_salenum:self.submitModel.goods_salenum clinic_id:self.submitModel.clinic_id name:self.submitModel.name recommend:self.submitModel.recommend current:self.page size:kMaxRequestSize success:^(NSURLSessionDataTask *task, id responseObject, Responese *respone) {
        
        if(self.page == 1)
            [self.dataSource removeAllObjects];
        NSArray *array = [NSArray yy_modelArrayWithClass:[TPHomeModel class] json:respone.data[@"data"]];
        [self.dataSource addObjectsFromArray:array];
        [self refreshSccuess];
        
        if(self.dataSource.count == 0){
            
            [self.collectionView tp_addNotMsg:@"Sorry，还没有数据哦!"];
        }
        
        
    } failure:^(NSURLSessionDataTask *task, NSError *error, Responese *respone) {
        [self refreshFail];
    } nonetWork:^(NSURLSessionDataTask *task, NSError *error) {
        [self refreshFail];
    }];
    
    
}


- (void)click {
    
    [self.collectionView reloadData];
}

//根据item的宽度与indexPath计算每一个item的高度
- (CGFloat)waterfallLayout:(XRWaterfallLayout *)waterfallLayout itemHeightForWidth:(CGFloat)itemWidth atIndexPath:(NSIndexPath *)indexPath {
    //根据图片的原始尺寸，及显示宽度，等比例缩放来计算显示高度
    
    return 230.0;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    TPCasesCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TPCasesCell" forIndexPath:indexPath];
    cell.indexPath = indexPath;
    cell.model = self.dataSource[indexPath.row];
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    TPProductDetailViewController *detailVC = [TPProductDetailViewController new];
    detailVC.model = self.dataSource[indexPath.row];
    [self.navigationController pushViewController:detailVC animated:YES];
    
}




-(void)refreshSccuess{
    if(self.page == 1){
        [self.collectionView xw_endPullToRefresh];
    }else{
        [self.collectionView xw_endInfinityRefreshAnimation:NO];
    }
    
    [self.collectionView reloadData];
    
    
}
-(void)refreshFail{
    if(self.page == 1){
        [self.collectionView xw_endPullToRefresh];
    }else{
        [self.collectionView xw_endInfinityRefreshAnimation:NO];
    }
    
    if(self.page > 1)
        self.page --;
    
    [self.tableView reloadData];
}


@end
